import * as d3 from "d3";
/*
* 来自: https://bl.ocks.org/mbostock/f6a3a2265a0c510acd80
* */
let connected_particles_II = (function () {

    let canvas = undefined;
    let context = undefined;
    let radius = undefined;
    let tau = undefined;
    let minDistance2 = undefined;
    let maxDistance2 = undefined;
    let width = undefined;
    let height = undefined;
    let n = undefined;
    let particles =undefined;
    let maxDistance = undefined;
    let minDistance = undefined;
    let myTimer = undefined;

    let init = function (canvasId,_w,_h,r) {
        canvas = document.querySelector('#'+canvasId);
        context = canvas.getContext("2d");
        width = _w;
        height = _h;
        radius = r;
        minDistance = 80;
        maxDistance = 100;
        minDistance2 = minDistance * minDistance;
        maxDistance2 = maxDistance * maxDistance;
        let density = Math.floor((width*height)/30000);
        canvas.width = width;
        canvas.height = height;
        tau = 2 * Math.PI;
        n = density;
        particles = new Array(n);

        for (let i = 0; i < n; ++i) {
            particles[i] = {
                x: width * Math.random(),
                y0: height * Math.random(),
                v: .1 * (Math.random() - .5)
            };
        }
    };

    let run = function (canvasId,_w,_h,r,lineWidth,lineColor) {
        /*
        * canvasId: canvas的id
        * _w: canvas的宽度
        * _h: canvas的高度
        * r: 点的半径
        * lineWidth: 线的宽度
        * lineColor: 线的颜色
        * */
        init(canvasId,_w,_h,r);
        myTimer = d3.timer(function(elapsed) {
            // console.log('run');
            context.clearRect(0, 0, width, height);

            context.lineWidth = lineWidth;
            context.strokeStyle = lineColor||"#777";
            for (let i = 0; i < n; ++i) {
                let pi = particles[i];
                for (let j = i + 1; j < n; ++j) {
                    let pj = particles[j],
                        dx = pi.x - pj.x,
                        dy = pi.y - pj.y,
                        d2 = dx * dx + dy * dy;
                    if (d2 < maxDistance2) {
                        context.globalAlpha = d2 > minDistance2 ? (maxDistance2 - d2) / (maxDistance2 - minDistance2) : 1;
                        context.beginPath();
                        context.arc((pi.x + pj.x) / 2, (pi.y + pj.y) / 2, Math.sqrt(d2) / 2, 0, tau);
                        context.stroke();
                    }
                }
            }
            context.globalAlpha = 1;

            for (let i = 0; i < n; ++i) {
                let p = particles[i];
                p.y = p.y0 + elapsed * p.v;
                if (p.y > height + maxDistance){
                    p.x = width * Math.random();
                    p.y0 -= height + 2 * maxDistance;
                }
                else if (p.y < -maxDistance){
                    p.x = width * Math.random();
                    p.y0 += height + 2 * maxDistance;
                }
                context.beginPath();
                context.arc(p.x, p.y, radius, 0, tau);
                context.fill();
            }
        });
    };

    let stop = function () {
        /*
        * 停止动画
        * */
        myTimer.stop();
    };

    return {
        run:run,
        stop:stop
    }
})();

export default connected_particles_II;

